package com.haierp.service.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.haierp.mapper.ScaleMapper;
import com.haierp.mapper.ScaleTypeMapper;
import com.haierp.model.Scale;
import com.haierp.model.ScaleType;
import com.haierp.service.IScaleTypeService;

@Service
public class ScaleTypeServiceImpl extends SuperServiceImpl<ScaleTypeMapper, ScaleType>  implements IScaleTypeService {

	private List<ScaleType> scaleTypeList;
	
	@Autowired
	private ScaleTypeMapper scaleTypeMapper;
	
	@Autowired
	private ScaleMapper scaleMapper;
	
	@PostConstruct
	public void initScaleTypeList() {
		scaleTypeList = scaleTypeMapper.selectList(null);
		Map<Long,ScaleType> map = new HashMap<>();
		scaleTypeList.forEach(
				scaleType->map.put(scaleType.getId(), scaleType)
				);
		
		List<Scale> scaleList = scaleMapper.selectList(null);
		scaleList.forEach(scale->{
			ScaleType st = map.get(scale.getTypeId());
			st.getScaleList().add(scale);
		});
		//对list进行排序
		scaleTypeList.forEach(
			scaleType->{
				List<Scale> scaleListSeq = scaleType.getScaleList();
				Collections.sort(scaleListSeq, new Comparator<Scale>(){
		            public int compare(Scale arg0, Scale arg1) {
		                return arg0.getSeq().compareTo(arg1.getSeq());
		            }
		        });
			}
		);
	}

	@Override
	public List<ScaleType> getScaleTypeList() {
		return scaleTypeList;
	}
	

}
